<?php
	error_reporting(0);
	session_start();
	if(!session_is_registered(myusername)){
	header("location:main_login.php");
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Supermarket</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>
    
<div id="container" class="clearfix">
    
    <h1>HEMART B10<em> CABANG<?php echo " $_SESSION[namacabang]";?></em></h1>
	<h2></h2>
     
	<div id="content">
		
		<h3>Pencatatan Transaksi Pembelian</h3>
		
		<?php
			$dbconn = pg_connect ("host=kamboja.cs.ui.ac.id port=5432 dbname=anindita.sekarini user=anindita.sekarini password=alibasjah91");
			if (!$dbconn) {
				echo "An error occured.\n";
				exit;
			}
			$_SESSION['kodeb'] = $_SESSION['kodebeli'];
			$jumlah = 0;
			$produk = $_POST['produk'];
			$kuantitas = $_POST['kuantitas'];
			$num = (int)$kuantitas;
			$pilihan = $_POST['pilihan'];
			$harga = pg_query($dbconn, "select harga from supermarket.harga natural join supermarket.produk
										where nama = '$produk'");
			$row = pg_fetch_row($harga);
			$kodeproduk = pg_query($dbconn, "select kode_produk from supermarket.produk where nama = '$produk'");
			$kodep = pg_fetch_row($kodeproduk);		
			//echo "KODE: $_SESSION[kodebeli]";
					
			if ($pilihan == "Tambah Transaksi") {
				if (!preg_match("/[^0-9]/",$kuantitas)){
					if($kuantitas!="0") {
						$exist = pg_query($dbconn, "select * from supermarket.pembelian_produk 
								  where kode_produk= '$kodep[0]' and kode_pembelian='$_SESSION[kodeb]'");
						$count_ex=pg_num_rows($exist);
						if ($count_ex==0){
							$insert_pembelian_produk = pg_query($dbconn, "insert into supermarket.pembelian_produk values
														   ('$_SESSION[kodeb]','$kodep[0]','$num','$row[0]')");
							$subtotal = $num * $row[0];
							$insert = pg_query($dbconn, "insert into supermarket.temp values ('$produk','$num','$subtotal')");
							echo "<br/>Untuk produk $produk dengan kuantitas $kuantitas memiliki <b>subtotal: $subtotal</b><br/><br/>";
							echo "<br/><br/><a href = 'pencatatan3.php'>Lanjut</a>";
						}
						else {
							echo "<label style=color:#FF0000>transaksi sudah ada!!</label>";
							echo "<br/><br/><a href = 'pencatatan3.php'>Masukkan data transaksi lain</a>";
						}
					}
					else {
						echo "<label style=color:#FF0000>kuantitas belum diisi!!</label>";
						echo "<br/><br/><a href = 'pencatatan3.php'>Masukkan data transaksi</a>";
					}
				}
				else {
					echo "<label style=color:#FF0000>kuantitas $kuantitas invalid!!</label>";
					echo "<br/><br/><a href = 'pencatatan3.php'>Masukkan data transaksi</a>";
				}
			}
			else if ($pilihan == "Selesai Transaksi") {
           if (!preg_match("/[^0-9]/",$kuantitas)){
               if($kuantitas!="0") {
                          $exist = pg_query($dbconn, "select * from supermarket.pembelian_produk
                                   where kode_produk= '$kodep[0]' and kode_pembelian='$_SESSION[kodeb]'");
                          $count_ex=pg_num_rows($exist);
                          if ($count_ex==0){
                              $insert_pembelian_produk = pg_query($dbconn, "insert into supermarket.pembelian_produk values
                                     ('$_SESSION[kodeb]','$kodep[0]','$num','$row[0]')");
                              $subtotal = $num * $row[0];
                              $insert = pg_query($dbconn, "insert into supermarket.temp values ('$produk','$num','$subtotal')");
                          }
                          else {
                                echo "<label style=color:#FF0000>transaksi sudah ada!!</label>";
                                echo "<br/><br/><a href = 'pencatatan3.php'>Masukkan data transaksi lain</a>";
                          }
                 }
                 else {
                     echo "<label style=color:#FF0000>kuantitas belum diisi!!</label>";
                     echo "<br/><br/><a href = 'pencatatan3.php'>Masukkan data transaksi</a>";
                 }
            }
            else {
                 echo "<label style=color:#FF0000>kuantitas $kuantitas invalid!!</label>";
                 echo "<br/><br/><a href = 'pencatatan3.php'>Masukkan data transaksi</a>";
            }
				$transaksi = pg_query($dbconn, "select * from supermarket.temp");
				echo "<br/>Transaksi yang tercatat:<br/><br/>
				<table>
				<th>Produk</th>
				<th>Kuantitas</th>
				<th>Subtotal</th>";
				
				for ( $counter = 0; $row = pg_fetch_row( $transaksi ) ; $counter++ )
				{
						print( "<tr>" );

						print( "<td>$row[0]</td>" );
						print( "<td>$row[1]</td>" );
						print( "<td align='right'>$row[2]</td>" );

						print( "</tr>" );				
				}
				
				$total = pg_query($dbconn, "select sum(subtotal) from supermarket.temp");
				$res = pg_fetch_row($total);
				echo "<tr><td>Total Transaksi</td><td></td><td align='right'>$res[0]</td></tr></table>";
				$destroy = pg_query($dbconn, "drop table supermarket.temp");

			}
		
		
		?>

	</div>
		 
			<ul id="nav">
				<li><a href="login_success.php">Home</a></li>
				<?php 
					if ($_SESSION['jabatan'] == 'Admin') 
					echo "<li><a href='form_akun.php'>Buat akun</a></li>";
				?>
				<li><a href="q1.php">List Daftar Cabang</a></li>
				<?php 
					if ($_SESSION['jabatan'] == 'Manajer Operasional')
					echo "<li><a href='pencatatan1.php'>Pencatatan Transaksi Pembelian</a></li>";
				?>
				<li><a href="daftar_transaksi1.php">Daftar Transaksi Pemasok</a></li>
				<li><a href="rekapitulasi1.php">Rekapitulasi Transaksi Pembelian</a></li>
				<li><a href="produk_terlaku.php" >Daftar Produk Terlaku</a></li>
				<li><a href="B10.php" title="View B10 info">About us</a></li>
				<li><a href="logout.php">Logout</a></li>
			</ul>
				
		</div>
		 
		<address>
			&copy; 2008 All Rights Reserved. Designed by <a href="http://www.caddoo.net" title="Portfolio of Matthew Caddoo">Matthew Caddoo</a> for <a href="http://www.zymic.com" title="Zymic Free Templates">Zymic</a> <a href="http://www.zymic.com/free-templates">Free Templates</a>. <a href="http://www.zymic.com/free-web-hosting/">Free Web Hosting</a>.
		</address>

</body>
</html>
